# streamer.py - Jukee
#
# Copyright (C) 2008 Ruben Medellin <ruben.medellin.c@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the
#    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#    Boston, MA  02111-1307  USA
 
"""
Contains the classes needed to send a file over a tcp network.
"""

import gobject, pygst
pygst.require("0.10")
import gst
from jukee.utils.utils import debug, warn, error

#################### Streamer ####################

class Streamer:
  """ Class that represents the jukebox streamer, at the client side. """

  def __init__(self, host, port, fileloc):
    """
    Sets up the pipeline for sending the stream with the given parameters.
    Pipeline construction: [ filesrc ! tcpclientsink ]
    """

    #The main pipeline
    self.pipeline = gst.Pipeline("client")

    # The elements of the pipeline
    self.src = gst.element_factory_make("filesrc", "source")
    self.client = gst.element_factory_make("tcpclientsink", "client")

    # Set up properties
    self.src.set_property("location", fileloc)
    self.client.set_property("host", host)
    self.client.set_property("port", port)

    # Add and link the elements altogether
    self.pipeline.add(self.src)
    self.pipeline.add(self.client)

    self.src.link(self.client)

    # Internal method for watching the pipeline bus for messages
    def watch_bus_event(bus, message, pipeline):
      t = message.type
      if t == gst.MESSAGE_EOS:
        debug("End of stream detected on " + str(message.src))
        ## CODE DIDNT WORK
        # self.client.post_message(gst.message_new_eos(self.src))
        # self.pipeline.post_message(gst.message_new_eos(self.pipeline))
        self.loop.quit()
      elif t == gst.MESSAGE_ERROR:
        err, deb = message.parse_error()
        error("GST ERROR " + str(err) + " - " + str(deb))
      else:
        debug("GST MESSAGE: " + str(message.type))
      return True
    
    self.pipeline.get_bus().add_watch(watch_bus_event, self.pipeline)
    
    # The player client is ready to send data
    debug("Client ready")

    # Create the main loop object
    self.loop = gobject.MainLoop()
    debug("Main loop ready")


  def play(self):
    """
    Sets the pipeline to playing state and sends data to server.
    """
    if not self.pipeline.set_state(gst.STATE_PLAYING):
      error("Error playing pipeline")
    else:
      try:
        self.loop.run()
      except:
        error("Error during main loop")
        self.pipeline.set_state(gst.STATE_NULL)

########################################

if __name__ == '__main__':
  client = Streamer("localhost", 8080, "/home/chubas/Desktop/V.mp3")
  client.play()
